package com.moresir.validatetools.rules;

import com.moresir.validatetools.impl.Rule;
import com.moresir.validatetools.impl.RuleDescriptor;
import com.moresir.validatetools.rules.list.number.RuleDouble;
import com.moresir.validatetools.rules.list.number.RuleInt;
import com.moresir.validatetools.rules.list.number.RuleLongMax;
import com.moresir.validatetools.rules.list.number.RuleLongMin;
import com.moresir.validatetools.rules.list.other.*;
import com.moresir.validatetools.rules.list.str.RuleNumeric;
import com.moresir.validatetools.rules.list.str.RuleValid;
import lombok.AllArgsConstructor;

import java.util.Optional;
import java.util.stream.Stream;

@AllArgsConstructor
public enum DefaultRules implements RuleDescriptor {

    /// STRING RULES
//    EMAIL(RuleEmail.class, "email", 0, ":attr is not an email."),
    BLANK(RuleValid.class, "valid", 0, ":attr 不是一个有效的值",":attr is not a valid value."),
    NUMERIC(RuleNumeric.class, "numeric", 0, ":attr 值不是一个数值型",":attr is not a numeric string."),

    /// NUMBER RULES
    INT(RuleInt.class, "int", 0, ":attr 值必须是int型",":attr is not a integer."),
    DOUBLE(RuleDouble.class, "double", 0, ":attr 值必须是double型",":attr is not a double."),
    LONG_MIN(RuleLongMin.class, "min", 1, ":attr 必须大于 :1",":attr must bigger than :1"),
    LONG_MAX(RuleLongMax.class, "max", 1, ":attr 必须小于等于 :1",":attr must smaller than or equal :1"),

    /// OTHER RULES
    CONTAIN(RuleContain.class, "contain", 1, ":attr 值必须包含 :1 ",":attr does not contain :1"),
    DATE_FORMAT(RuleDateFormat.class, "dateFormat", 1, ":attr 值的格式不匹配 :1",":attr does not match format :1"),
    IN(RuleIn.class, "in", 1, ":attr 值范围必须在 :1 中",":attr does not in :1"),
    OPTIONAL(RuleOptional.class, "optional", -1, ":attr 是可选项",":attr is optional"),
    VALID_USERNAME(RuleValidUserName.class, "validUserName", -1, ":attr 不是一个有效的用户名", ":attr is not a valid userName");

    private Class<? extends Rule> rule;
    private String definer;
    private int paramsCount;
    private String defaultMessage;
    private String enMessage;

    @Override
    public Class<? extends Rule> getRule() {
        return rule;
    }

    @Override
    public String getDefaultMessage() {
        return defaultMessage;
    }

    @Override
    public void setDefaultMessage(String ruleName, String message) {
        this.defaultMessage = message;
    }


    @Override
    public String getRuleName() {
        return definer;
    }

    @Override
    public int getParamsCount() {
        return paramsCount;
    }

    @Override
    public String toString() {
        return "DefaultRules{" +
                "rule=" + rule +
                ", defaultMessage='" + defaultMessage + '\'' +
                ", definer='" + definer + '\'' +
                ", paramsCount=" + paramsCount +
                '}';
    }

    public static Optional<RuleDescriptor> getByRuleName(String ruleName) {
        return Stream.of(DefaultRules.values())
                .filter(e -> e.getRuleName().equals(ruleName))
                .map(e -> (RuleDescriptor)e).findFirst();
    }
}
